package com.controller

import com.aracoix.model.CcUser
import com.aracoix.model.NearbyPrd
import com.eova.common.base.BaseController
import com.eova.common.utils.xx
import com.jfinal.plugin.activerecord.Db
import com.jfinal.plugin.activerecord.Record

/**
 * Created by Administrator on 2019/1/14.
 */
class RegisterController : BaseController(){
    companion object {
        const val HAS = 100 // 已注册
        const val Error = 101 //错误
        const val Empty = 102 //账号密码空
        const val NotRegist = 103 // 没注册
        const val Wrong = 104 // 账号密码错误
    }
    //登录
    fun login(){
        val bean = BackBean()
        val map = postMap()
        val name = getPara("name")
        val pass = getPara("pass")
        if (name.isNullOrBlank()||pass.isNullOrBlank()) {
            bean.code = Empty
            renderJson(bean)
            return
        }
        val find = CcUser.dao.findFirst("select * from cc_user where name = '$name'")
        if (find != null) {
            if (find.pass.equals(pass)){
                bean.obj = find
                renderJson(bean)
            }else{
                bean.code = Wrong
                renderJson(bean)
            }

        }else{
            bean.code = NotRegist
            renderJson(bean)
        }
    }
    // 注册
    fun register(){
        val bean = BackBean()
        val name = getPara("name")
        val pass = getPara("pass")
        val nickName = getPara("nickName")
        val key = getPara("key")
        if (name.isNullOrBlank()||pass.isNullOrBlank()) {
            bean.code = Empty
            renderJson(bean)
            return
        }
        val find = CcUser.dao.find("select * from cc_user where name = '$name'")
        if (find != null) {
            bean.code = HAS
        }else{
            val save = Db.use(xx.DS_MAIN).save("cc_user", Record().set("name", name).set("pass", pass).set("nim_token",key).set("nickname",nickName))
            if (!save) {
                bean.code = Error
            }
        }
        renderJson(bean)
    }
    // 附近乘车点
    fun getNearPort(){
        val bean = BackBean()
        val find = NearbyPrd.dao.find("select * from nearby_prd")
        bean.obj = find
        renderJson(bean)
    }

}
